package com.zyg.order.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyg.common.entity.PayVo;
import com.zyg.common.entity.TbPayLog;
import com.zyg.order.entity.*;
import com.zyg.order.service.PayService;
import com.zyg.order.service.TbAddressService;
import com.zyg.order.service.TbOrderService;
import com.zyg.order.service.TbPayLogService;
import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/17-15:02
 * ------------------------------
 */
@Controller
public class OrderController {
    @Autowired
    private TbOrderService orderService;
    @Autowired
    private TbAddressService addressService;
    @Autowired
    private PayService payService;
    @Autowired
    private TbPayLogService payLogService;
    //1. 默认页面
    @GetMapping({"/","orderInfo"})
    public String orderInfo(Model model){
        //1.1 得到登录用户名
        // String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //1.2 根据登录名得到收货人地址列表
        List<TbAddress> addressList = addressService.list(new QueryWrapper<TbAddress>().eq("user_id", "zelin"));
        model.addAttribute("addressList",addressList);



        //1.3 根据用户名从购物车取出数据
        List<Cart> cartList = orderService.findCartList("zelin");
        model.addAttribute("cartList",cartList);

        return "orderInfo";
    }

    //2. 保存订单
    @PostMapping("order/add")
    public String saveOrder(TbOrder order,Model model){
        //2.1 得到登录用户名
        //String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //2.2 保存订单
        orderService.save("zelin",order);
        //2.3 从redis中得到支付日志
        TbPayLog payLog = orderService.getPayLogFromRedis("zelin");
        System.out.println("payLog = " + payLog);
        //2.4 将得到的日志对象放到model中，并跳转到支付页面
        model.addAttribute("payLog",payLog);
        return "pay";
    }

    //3. 点击支付按钮
    @GetMapping("order/pay/{type}")
    @ResponseBody
    public String pay(@PathVariable int type, String outTradeNo) throws AlipayApiException {
        //3.1 得到登录名
        // String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //3.2 根据登录名构造PayVo请求参数
        PayVo vo = orderService.findPayVoByname("zelin",outTradeNo);
        //3.3 开始支付
        String result  = payService.pay(vo,type);
        return result;
    }

    //4. 得到登录用户的订单列表(支付宝支付的同步通知)
    @GetMapping("orderlist")
    public String orderList(Model model){
        //4.1 得到登录名
        // String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //4.2 根据登录用户查询此用户的订单列表
        List<TbOrder> orderList = orderService.findOrderListByname("zelin");
        //4.3 将得到订单列表放到model中
        model.addAttribute("orderList",orderList);
        //4.4 返回
        return "orderlist";
    }
    @RequestMapping("order/findPayPlog/{id}")
    @ResponseBody
    public TbPayLog findPayLog(@PathVariable("id") String id){
        return orderService.findPayLog(id);
    }

    //1. 修改支付日志
    @RequestMapping("order/updatePaylog")
    @ResponseBody
    public void updatePayLog(@RequestBody TbPayLog payLog){
        System.out.println("payLog------------------> = " + payLog);
        payLogService.updateById(payLog);
    }

    //2. 修改订单状态
    @RequestMapping("order/updateOrder/{id}")
    @ResponseBody
    public void updateOrder(@PathVariable("id") String id){
        TbOrder order = orderService.getById(id);
        order.setStatus("2");       //己支付
        orderService.updateById(order);
    }

    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}
